package com.hq.fxh.No3_TuiGuang.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.HttpApi.ArticleServiceImpl;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetAllRankRes;
import com.hq.fxh.Modle.RankBean;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No0_ChengQu.Activity.News_AddAD_DetailActivity;
import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.No3_TuiGuang.Adaoter.Ladapter_HotNews;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.CommonRes;
import com.hq.fxh.R;
import com.hq.fxh.Utils.IntentUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.io.Serializable;
import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.List;

import static android.R.attr.data;
import static cn.jiguang.analytics.android.api.JAnalyticsInterface.mContext;
import static cn.jpush.im.android.tasks.GetUserInfoListTask.IDType.uid;
import static com.hq.fxh.R.id.recyclerView;
import static com.hq.fxh.R.id.refresh;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/10/9
 * @Email xlwenhao1858@sina.com
 */


public class HotNew_Activity extends BaseActivity implements View.OnClickListener {
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    protected PullToRefreshListView listFenSi;
    int page = 1;
    private Ladapter_HotNews adapter;
    private List<RankBean> msgData;

    @Override
    protected void initView() {
        toolbarImageBack = (ImageView) findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(HotNew_Activity.this);
        toolBarTvTitle = (TextView) findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) findViewById(R.id.toolbar_Layout_More);
        toolbarContent = (LinearLayout) findViewById(R.id.toolbar_Content);
        listFenSi = (PullToRefreshListView) findViewById(R.id.list_FenSi);

    }

    @Override
    protected void initData() {
        getArticleRank();
        toolBarTvMore.setVisibility(View.INVISIBLE);
        toolBarTvTitle.setText("热文榜");

        listFenSi.setMode(PullToRefreshBase.Mode.BOTH);
        listFenSi.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                getArticleRank();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                ++page;
                getArticleRank();
            }
        });

        listFenSi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

                RankBean rankBean = msgData.get(i);
                ArticleServiceImpl.pasterNewsUrl(rankBean.getSource_url(), new NetworkSubscriber<CommonRes>() {
                    @Override
                    public int onSuccess(CommonRes data) {
                        super.onSuccess(data);
                        if (data != null && data.getCode() == 0) {
                            Bundle bundle = new Bundle();
                            if (i > 1) {
                               int j = i - 1;
                                bundle.putString("NewsUrl", msgData.get(j).getSource_url());
                            } else {
                                bundle.putString("NewsUrl", msgData.get(i).getSource_url());

                            }

                            if (SharedPreferenceUtils.getMemberId(HotNew_Activity.this) == 0) {
                                startActivity(new Intent(HotNew_Activity.this, LoginActivity.class));
                            } else {
                                Intent intent = new Intent(HotNew_Activity.this, News_AddAD_DetailActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        } else {
                            ToastUtils.show(mContext, data.getMsg());
                        }
                        return 0;
                    }
                });
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_fen_si;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.toolbar_Layout_back) {
            finish();

        }
    }


    private void getArticleRank() {


        //分享榜 邀请榜 设置数据
        UserServiceImpl.getHotArticle(3, new NetworkSubscriber<GetAllRankRes>() {
            @Override
            public int onSuccess(GetAllRankRes data) {
                listFenSi.onRefreshComplete();
                super.onSuccess(data);
                if (data.getCode() == 0) {
                    if (msgData == null) {
                        msgData = new ArrayList<>();
                    }
                    if (page == 1) {
                        msgData.clear();
                    }
                    List<RankBean> data1 = data.getData();
                    msgData.addAll(data1);
                    if (adapter == null) {
                        adapter = new Ladapter_HotNews(msgData);
                        listFenSi.setAdapter(adapter);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.show(HotNew_Activity.this, data.getMsg());
                }
                return 0;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

                listFenSi.onRefreshComplete();
            }
        });
    }
}
